﻿using System;

using System.Globalization;
using System.Security.Cryptography.X509Certificates;

namespace FID_LearningDataCollection
{

    /// <summary>
    /// 压缩机工作状态
    /// </summary>
    public class WorkingState
    {
        /// <summary>
        /// 上一周期采样时间
        /// </summary>
        public DateTime LastSCTime;

        /// <summary>
        /// 上一周期采样Freq
        /// </summary>
        public float LastSendingFreq;

        /// <summary>
        /// 上一周期吸气端压力
        /// </summary>
        public float LastP1;

        /// <summary>
        /// 上一周期排气端压力
        /// </summary>
        public float LastP2;

        /// <summary>		 
        /// 采样时间
        /// </summary>
        public DateTime SCTime;

        /// <summary>
        /// 吸气压力
        /// </summary>
        public float P1;

        /// <summary>
        /// 排气压力
        /// </summary>
        public float P2;

        /// <summary>
        /// 压缩机工作频率
        /// </summary>
        public float Freq;

        public float FIDFreq;

        //public float ActualFreq;

        /// <summary>
        /// 压缩机工作电流
        /// </summary>
        public float ECurrent;

        /// <summary>
        /// 吸气温度
        /// </summary>
        public float T1;

        /// <summary>
        /// 排气温度
        /// </summary>
        public float T2;

        /// <summary>
        /// 环境温度
        /// </summary>
        public float TEnvir;

        /// <summary>
        /// 是否为控制点，若为控制点，则为true;否则为false
        /// </summary>
        public bool canControl;

        /// <summary>
        /// 吸气过热度
        /// </summary>
        public float Superheat;

        /// <summary>
        /// 限制标志，1:强制停机，2:慢速增频，4：禁止增频，8：强制减频
        /// </summary>
        public int LimitFlag;

        /// <summary>
        /// 变频器输入电流(1次侧)
        /// </summary>
        public float ICurrent;

        /// <summary>
        /// 当前控制点（吸气压力）
        /// </summary>
        public float P1Set;

        /// <summary>
        /// 当前变频器读写 R：读（0），W:写（1）
        /// </summary>
        public int MainBoardWRStatus;

        /// <summary>
        /// PID计算频率
        /// </summary>
        public float PIDOut;

        /// <summary>F
        /// 写出频率
        /// </summary>
        public float WriteOutFreq;

        /// <summary>
        /// 写出后毫秒数
        /// </summary>
        public int WriteOutTime;

        /// <summary>
        /// 吸气压力低停机设定点
        /// </summary>
        public float P1StopSet;

        /// <summary>
        /// 最小运行频率
        /// </summary>
        public float MinFreq;

        /// <summary>
        /// 最大运行频率
        /// </summary>
        public float MaxFreq;

        /// <summary>
        /// 最小频率停机延时  
        /// </summary>
        public int FreqAtMinDelay;

        /// <summary>
        /// 当读入频率Freq=0时，可开机；当写入频率Freq=0时，立即停机
        /// </summary>
        public const int CAN_START_FREQ = 0;

        /// <summary>
        // 停机频率
        /// </summary>
        public const int CANNOT_START_FREQ = -1;

        /// <summary>
        // 回油频率
        /// </summary>
        public const int REPAIR_FREQ = -2;

        public float FIDCalcFreq; // FID计算的频率
        public float InferFreq; // 推断之后的频率
        public float lasIncrement;
        public float curIncrement;
        public float futIncrement;

        public string AccStatus; // 加减速状态

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sTime">采样时间</param>
        /// <param name="p1">吸气压力</param>
        /// <param name="p2">排气压力</param>
        /// <param name="freq">工作频率</param>
        /// <param name="eCurrent">工作电流</param>
        /// <param name="t1">吸气温度</param>
        /// <param name="t2">排气温度</param>
        /// <param name="tEnvir">环境温度</param>
        /// <param name="lastSCTime"></param>
        /// <param name="lastP1"></param>
        /// <param name="lastP2"></param>
        /// <param name="superheat"></param>
        /// <param name="pidOut"></param>
        /// <param name="writeOutFreq"></param>
        /// <param name="limitFlag"></param>
        /// <param name="iCurrent"></param>
        /// <param name="p1Set"></param>
        /// <param name="mainBoardWRStatus"></param>
        /// <param name="writeOutTime"></param>
        /// <param name="stopPressure"></param>
        /// <param name="freqMin"></param>
        /// <param name="freqMax"></param>
        /// <param name="freqAtMinDelay"></param>									 
        public WorkingState(DateTime sTime,
                            float p1,
                            float p2,
                            float freq,
                            float eCurrent,
                            float t1,
                            float t2,
                            float tEnvir,
                            DateTime lastSCTime,
                            float lastP1,
                            float lastP2,
                            float superheat,
                            float pidOut,
                            float writeOutFreq,
                            int limitFlag,
                            float iCurrent,
                            float p1Set,
                            int mainBoardWRStatus,
                            int writeOutTime,
                            float stopPressure,
                            float freqMin,
                            float freqMax,
                            int freqAtMinDelay,
                            float FIDFreq
                            )
        {
            this.SCTime = sTime;
            this.P1 = p1;
            this.P2 = p2;
            //this.Freq = freq;

            // #2023/11/22 18:24 修改真实频率
            this.FIDFreq = FIDFreq;
            this.Freq = freq;

            this.ECurrent = eCurrent;
            this.T1 = t1;
            this.T2 = t2;
            this.TEnvir = tEnvir;
            this.LastSCTime = lastSCTime;
            this.LastP1 = lastP1;
            this.LastP2 = lastP2;
            this.Superheat = superheat;
            this.PIDOut = pidOut;
            this.WriteOutFreq = writeOutFreq;
            this.LimitFlag = limitFlag;
            this.ICurrent = iCurrent;
            this.P1Set = p1Set;
            this.MainBoardWRStatus = mainBoardWRStatus;
            this.WriteOutTime = writeOutTime;
            this.P1StopSet = stopPressure;
            this.MinFreq = freqMin;
            this.MaxFreq = freqMax;
            this.FreqAtMinDelay = freqAtMinDelay;

            //该变量将在FIDer类中赋值
            this.canControl = false;
        }

        public WorkingState(float p1, float freq, string accStatus)
        {
            this.P1 = p1;
            this.Freq = freq;
            this.AccStatus = accStatus;
        }

        public WorkingState()
        {
        }


        /// <summary>
        /// 判断压缩机是否为开机状态，即Freq>0
        /// </summary>
        /// <returns>开机状态，true；否则false</returns>
        public bool IsWorking()
        {
            if (this.Freq == WorkingState.CANNOT_START_FREQ ||
                this.Freq == WorkingState.CAN_START_FREQ)
                return false;
            else
                return true;
        }

        public string GTime()
        {
            return this.SCTime.ToString("O", new CultureInfo("en-us"));
        }

        /// <summary>
        /// 判断是否可以开机
        /// </summary>
        /// <returns>开机状态，true；否则false</returns>
        public bool canStart()
        {
            if (this.Freq == WorkingState.CANNOT_START_FREQ)
                return false;
            else
                return true;
        }

        /// <summary>
        /// 采用时间的标准化输出
        /// </summary>
        public string FormatSCTime()
        {
            return this.SCTime.ToString("yyyy-MM-dd HH:mm:ss.fff", DateTimeFormatInfo.InvariantInfo);
        }


    }
}
